from PyQt4 import QtGui, QtCore
from custom_ui import ListWidget

class MainWindow(QtGui.QMainWindow):
  def __init__(self):
    QtGui.QMainWindow.__init__(self)

    self.main_splitter = QtGui.QSplitter()
    self.main_splitter.setChildrenCollapsible(False)
    self.setCentralWidget(self.main_splitter)

    self.__create_db_ui__()
    self.__create_img_ui__()
    #self.main_splitter.setStretchFactor(0, 1)
    self.main_splitter.setStretchFactor(1, 2)

  def __list_widget__(self):
    view = ListWidget()

    # the following two properties are needed
    # to show the item uniformly
    view.setUniformItemSizes(True)
    view.setWordWrap(True)

    return view

  def __create_img_ui__(self):

    self.toggle_img_view_btn = self.__tool_btn__('Toggle')
    self.add_img_btn = self.__tool_btn__('Add Image')
    self.add_img_folder_btn = self.__tool_btn__('Add Folder')

    hbox = QtGui.QHBoxLayout()
    hbox.setAlignment(QtCore.Qt.AlignLeft)
    hbox.addWidget(self.toggle_img_view_btn)
    hbox.addWidget(self.add_img_btn)
    hbox.addWidget(self.add_img_folder_btn)

    self.img_view = self.__list_widget__()

    # adjust items when the widget is resized,
    # this is needed for IconMode
    self.img_view.setResizeMode(QtGui.QListView.Adjust)

    # allow dragging, but under ListMode, new item
    # may be generated by doing so
    #self.img_view.setMovement(QtGui.QListView.Snap)

    vbox = QtGui.QVBoxLayout()
    vbox.addLayout(hbox)
    vbox.addWidget(self.img_view)

    img_group_box = QtGui.QGroupBox()
    img_group_box.setTitle('Images')
    img_group_box.setLayout(vbox)

    self.main_splitter.addWidget(img_group_box)

  def __create_db_ui__(self):
    self.new_db_btn = self.__tool_btn__('New')
    self.load_db_btn = self.__tool_btn__('Load')

    hbox = QtGui.QHBoxLayout()
    hbox.setAlignment(QtCore.Qt.AlignLeft)
    hbox.addWidget(self.new_db_btn)
    hbox.addWidget(self.load_db_btn)

    self.db_view = self.__list_widget__()

    vbox = QtGui.QVBoxLayout()
    vbox.addLayout(hbox)
    vbox.addWidget(self.db_view)

    db_group_box = QtGui.QGroupBox()
    db_group_box.setTitle('Database')
    db_group_box.setLayout(vbox)
    db_group_box.adjustSize()

    self.main_splitter.addWidget(db_group_box)

  def __tool_btn__(self, text): 
    btn = QtGui.QPushButton(text) 
    policy = QtGui.QSizePolicy.Fixed
    btn.setSizePolicy(policy, policy)
    return btn

if __name__ == '__main__':
  import sys
  app = QtGui.QApplication(sys.argv)
  window = MainWindow()
  window.show()
  sys.exit(app.exec_())
